/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.instructional.example.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.Style;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;

public class J42JPanel_04 extends JPanel {

    private static final long serialVersionUID = -853223545252673882L;
    private JTextComponent    editorPane       = null;
    private final JScrollPane scrollPane;
    private final JPanel      jPanel;
    private final PrintStream printStream;
    private final int         namedStyle       = 0;
    private Document          doc              = null;

    public J42JPanel_04() {
        this("");
    }

    private J42JPanel_04(final String title) {
        super();
        jPanel = this;
        editorPane = new JEditorPane();
        editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
        final JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(editorPane);
        scrollPane = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jPanel.setLayout(new BorderLayout());
        editorPane.setFont(super.getFont());
        editorPane.setBackground(Color.BLACK);
        editorPane.setForeground(Color.WHITE);
        ((JEditorPane) editorPane).setEditorKit(new StyledEditorKit());
        doc = editorPane.getDocument();
        final Border border = BorderFactory.createTitledBorder(title);
        jPanel.setBorder(border);
        jPanel.add(scrollPane);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream() {
            @Override
            synchronized public void write(final byte b[], final int off, final int len) {
                try {
                    final Style style = ((StyledDocument) doc).getStyle("" + namedStyle);
                    doc.insertString(doc.getLength(), new String(b, off, len), style);
                    ((JEditorPane) editorPane).setCaretPosition(doc.getLength() - 1);
                } catch (final BadLocationException e) {
                    e.printStackTrace();
                }
            }
        };
        printStream = new PrintStream(baos);
    }

    public void println(final String string) {
        printStream.println(string);
    }

    public void print(final String string) {
        printStream.print(string);
    }

    public PrintStream getPrintStream() {
        return printStream;
    }
}
